<?php
namespace app\index\controller;

use think\Controller;


class Jiaose extends Controller
{
   //查询
   public function index(){
    
		$data=db('auth_group')->paginate(10);
		//halt($data);
        
		//分页
        $page = $data->render();

		//计算多少条数据
		$count=count(db('auth_group')->select());

		return view('',['data'=>$data,'tiao'=>$count,'page'=>$page]);
   }
    
  //添加
  public function jiaoseadd(){

      //判断是否为post提交
	  if(request()->isAjax()){
	  $data=input('post.');
      //halt($data);
		$rs=db('auth_group')->insert($data);
		if($rs){
			 return array('code'=>1,'msg'=>'添加成功');
		}else{
			return array('code'=>0,'msg'=>'添加失败');
		}
	 }else{
		return view();
	  }
   }

   //删除
   public function delete(){
		$id=input('id');

		//权限判断
		
			$rs=db('auth_group')->where('id',$id)->find();
			$del=db('auth_group')->where('id',$id)->delete();
			if($del){
				return array('code'=>1,'msg'=>"删除成功");
			}else{
				return array('code'=>0,'msg'=>"删除失败");
			}
	   
    }

	//修改
     public function update(){
		$id=input('id');
        //halt($id);
        $rs=db('auth_group')
			->where('f_auth_group.id',$id)
            ->find();
			//halt($rs);
		if(request()->isAjax()){
			$id=input('id');
			$rs=db('auth_group')
				->where('id',$id)
				->update(['jname'=>input('jname'),'describe'=>input('describe')]);
			if($rs){
				return array('code'=>1,'msg'=>"修改成功");
			  }else{
				return array('code'=>0,'msg'=>"修改失败");
			  }
		 }else{

			return view('',['rs'=>$rs,'id'=>$id]);
		 }
	 }
	 //设置权限
	 public function setquan(){
	    
			$id=input('id');
			

			//查询角色
			$res=db('auth_group')->where('id',$id)->find();
			
			//将rules分割为数组
			$rules=$res['rules'];
			$arr=explode(',',$rules);

            //查出所有被选中的权限
			for($i=0;$i<count($arr);$i++){
				$data[]=db('auth_rule')
					->where('f_auth_rule.id',$arr[$i])
					->select();
			}
			//查出所有权限
			$result=db('auth_rule')->where('f_auth_rule.id','not in',$rules)->select();
			//halt($result);
		if(request()->isPost()){
            $id=input('hid');
			$value=input('post.access/a');//halt($value);
			$rules=implode(',',$value);
			$edit=db('auth_group')->where('id',$id)->update(['rules'=>$rules]);
			if($edit){
				return array('code'=>1,'msg'=>"设置成功");
			}
			else{
				return array('code'=>0,'msg'=>"设置失败");
			}
		}else{
			return view('',['res'=>$res,'data'=>$data,'result'=>$result]);
		}
	 }
}